<template>
  <div class="admin-layout">
    <header class="main-header">
        <div class="header-left">
       
          <h1 @click="goToDashboard">烈焰之雨的Blog</h1>
        </div>
        
        <div class="header-right">
          <span class="welcome-text">欢迎，{{ currentUser?.fullName || currentUser?.username }}</span>
          
          <!-- 操作按钮 -->
          <el-space :size="8">
            <el-button size="medium" @click="goHome">
              <el-icon><HomeFilled /></el-icon>
              返回首页
            </el-button>
            
            <el-button type="danger" size="medium" @click="showLogoutConfirm">
              <el-icon><SwitchButton /></el-icon>
              退出登录
            </el-button>
          </el-space>
        </div>
        
      </header>
      <div class="content-body">
    <aside class="sidebar">
      <div class="logo">

        <h3>管理后台</h3>
      </div>
      
      <el-menu
        :default-active="activeMenu"
        class="sidebar-menu"
        background-color="#2c3e50"
        text-color="#bdc3c7"
        active-text-color="#409eff"
        router
      >
        <el-menu-item index="/admin/dashboard">
          <el-icon><DataLine /></el-icon>
          <span>仪表板</span>
        </el-menu-item>
        
        <el-menu-item index="/admin/articles">
          <el-icon><Edit /></el-icon>
          <span>文章管理</span>
        </el-menu-item>
        
        <el-menu-item index="/admin/categories">
          <el-icon><PriceTag /></el-icon>
          <span>分类管理</span>
        </el-menu-item>
        
        <el-menu-item 
          v-if="currentUser && currentUser.isAdmin" 
          index="/admin/users"
        >
          <el-icon><User /></el-icon>
          <span>用户管理</span>
        </el-menu-item>
      </el-menu>
    </aside>
    
    <main class="main-content">

      
      <div class="content">
        <router-view />
      </div>
      
      <footer class="main-footer">
        <span>© 2025 烈焰之雨的Blog 管理后台</span>
        <span>Powered by Spring Boot & Element Plus</span>
      </footer>
    </main>
  </div>
  </div>
</template>

<script>
import { DataLine, Edit, PriceTag, User, HomeFilled, SwitchButton, MagicStick } from '@element-plus/icons-vue'
import { authAPI } from '@api/auth.js'
import { ElMessageBox } from 'element-plus'

export default {
  name: 'AdminLayout',
  components: {
    DataLine,
    Edit,
    PriceTag,
    User,
    HomeFilled,
    SwitchButton,
    MagicStick
  },
  computed: {
    currentUser() {
      return this.$store.state.user
    },
    activeMenu() {
      return this.$route.path
    }
  },
  methods: {
    async confirmLogout() {
      try {
        await ElMessageBox.confirm(
          '确定要退出登录吗？',
          '确认退出',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
        
        try {
          await authAPI.logout()
          // 清除本地存储的token
          localStorage.removeItem('jwt_token')
          this.$store.commit('clearUser')
          this.$store.commit('setInitialized', false)
          this.$router.push('/login')
        } catch (error) {
          console.error('退出登录失败:', error)
          // 即使请求失败，也清除本地状态
          localStorage.removeItem('jwt_token')
          this.$store.commit('clearUser')
          this.$store.commit('setInitialized', false)
          this.$router.push('/login')
        }
      } catch {
        // 用户取消
      }
    },
    
    async goHome() {
      await this.$router.push('/')
    },
    
    async goToDashboard() {
      await this.$router.push('/admin/dashboard')
    },
    
    async showLogoutConfirm() {
      await this.confirmLogout()
    },
    
    getPageTitle() {
      const route = this.$route.name
      const titles = {
        'ArticleManagement': '文章管理',
        'CategoryManagement': '分类管理',
        'UserManagement': '用户管理',
        'Dashboard': '仪表板'
      }
      return titles[route] || '管理后台'
    }
  }
}
</script>

<style scoped>
.admin-layout {
  display: flex;
  flex-direction: column;
  width: 100%;
  background: #f8f9fa;
  min-height: 100vh;
}

.content-body {
  display: flex;
  flex: 1;
  width: 100%;
}

.sidebar {
  width: 250px;
  min-width: 250px;
  max-width: 250px;
  flex-shrink: 0;
  background: #282a2d;
  color: white;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
  min-height: 100vh;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: #2c3e50;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo .el-icon {
  color: #409eff;
}

.logo h3 {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 700;
  color: white;
}

.sidebar-menu {
  border-right: none !important;
  flex: 1;
  min-height: 0;
}


.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0;
  background: #2a2a2a;
}

.main-header {
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg-primary) 100%);
  color: var(--text-black);
  box-shadow: var(--shadow-md);
  border-bottom: 3px solid;
  border-image: var(--gradient-rainbow) 1;
  position: relative;
  width: 100%;
  padding: 0 2rem;
  box-sizing: border-box;
}

.main-header::after {
  content: '';
  position: absolute;
  bottom: -3px;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--gradient-rainbow);
  filter: blur(8px);
  opacity: 0.6;
}

.header-left h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  background: var(--gradient-rainbow);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  cursor: pointer;
  transition: filter var(--transition-normal);
}

.header-left h1:hover {
  filter: drop-shadow(0 0 12px rgba(92, 225, 255, 0.6));
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-shrink: 0;
  max-width: calc(100% - 250px); /* 为左侧标题留出空间 */
  overflow: visible;
}

.welcome-text {
  color: var(--text-primary);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
  font-weight: 500;
  flex-shrink: 1;
}

/* 按钮样式调整，确保显示完整且样式一致 */
.header-right .el-button {
  height: 36px;
  padding: 0 16px;
  font-size: 14px;
  border-radius: 6px;
  transition: all 0.3s ease;
  white-space: nowrap;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background-color: transparent;
  border: 1px solid var(--color-primary);
  color: var(--color-primary);
}

/* 危险按钮默认样式 */
.header-right .el-button--danger {
  background-color: transparent;
  border-color: #f56c6c;
  color: #f56c6c;
}

.header-right .el-button--medium {
  height: 36px;
  padding: 0 16px;
  font-size: 14px;
}

.header-right .el-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 默认按钮悬停效果 */
.header-right .el-button:not(.el-button--danger):hover {
  background-color: var(--color-primary) !important;
  border-color: var(--color-primary) !important;
  color: white !important;
}

/* 危险按钮悬停效果 */
.header-right .el-button--danger:hover {
  background-color: #f56c6c !important;
  border-color: #f56c6c !important;
  color: white !important;
}


/* 确保所有按钮都有悬停效果 */
.header-right .el-button:hover .el-icon {
  color: inherit;
}

.content {
  flex: 1;
  overflow: auto;
  width: 100%;
  box-sizing: border-box;
  padding: 0.2rem;
  background: linear-gradient(135deg, #2a2a2a 0%, #1e3a8a 50%, #1b0246 100%);
  min-height: 0;
}

.main-footer {
  height: 48px;
  background: #3c3c3c;
  border-top: 1px solid #5a5a5a;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  color: #a3a6ad;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-layout {
    flex-direction: column;
  }
  
  .sidebar {
    width: 100%;
    height: auto;
  }
  
  .sidebar-nav {
    display: flex;
    overflow-x: auto;
  }
  
  .nav-item {
    white-space: nowrap;
    min-width: 120px;
  }
  

  
  .header-left h1 {
    font-size: 1.2rem;
  }
  
  .header-right {
    gap: 0.5rem;
    max-width: calc(100% - 150px);
  }
  
  .welcome-text {
    display: none;
  }
  
  .home-btn {
    padding: 0.4rem 0.8rem;
    font-size: 0.85rem;
  }
  
  .user-avatar-trigger {
    width: 36px;
    height: 36px;
    font-size: 0.9rem;
  }
  
  .user-card {
    min-width: 260px;
    right: -10px;
  }
  
  .card-header {
    padding: 0.75rem;
  }
  
  .card-avatar {
    width: 40px;
    height: 40px;
    font-size: 1rem;
  }
  
  .card-name {
    font-size: 0.9rem;
  }
  
  .card-role {
    font-size: 0.8rem;
  }
  
  .logout-action {
    padding: 0.4rem 0.6rem;
    font-size: 0.8rem;
  }
}
</style>
